package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab05Bo;
import org.dromara.yuanshidian.domain.vo.Ab05Vo;
import org.dromara.yuanshidian.domain.Ab05;
import org.dromara.yuanshidian.mapper.Ab05Mapper;
import org.dromara.yuanshidian.service.IAb05Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 个人账户总账Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab05ServiceImpl implements IAb05Service {

    private final Ab05Mapper baseMapper;

    /**
     * 查询个人账户总账
     *
     * @param id 主键
     * @return 个人账户总账
     */
    @Override
    public Ab05Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询个人账户总账列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 个人账户总账分页列表
     */
    @Override
    public TableDataInfo<Ab05Vo> queryPageList(Ab05Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab05> lqw = buildQueryWrapper(bo);
        Page<Ab05Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的个人账户总账列表
     *
     * @param bo 查询条件
     * @return 个人账户总账列表
     */
    @Override
    public List<Ab05Vo> queryList(Ab05Bo bo) {
        LambdaQueryWrapper<Ab05> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab05> buildQueryWrapper(Ab05Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab05> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab05::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab05::getDw01, bo.getDw01());
        lqw.eq(StringUtils.isNotBlank(bo.getDw02()), Ab05::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr01()), Ab05::getGr02, bo.getGr01());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab05::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab05::getGr03, bo.getGr03());
        lqw.eq(StringUtils.isNotBlank(bo.getGr04()), Ab05::getGr04, bo.getGr04());
        lqw.eq(bo.getGrzh01() != null, Ab05::getGrzh01, bo.getGrzh01());
        lqw.eq(bo.getGrzh02() != null, Ab05::getGrzh02, bo.getGrzh02());
        lqw.eq(bo.getGrzh03() != null, Ab05::getGrzh03, bo.getGrzh03());
        lqw.eq(bo.getGrzh04() != null, Ab05::getGrzh04, bo.getGrzh04());
        lqw.eq(bo.getGrzh05() != null, Ab05::getGrzh05, bo.getGrzh05());
        lqw.eq(bo.getGrzh06() != null, Ab05::getGrzh06, bo.getGrzh06());
        lqw.eq(bo.getGr10() != null, Ab05::getGr10, bo.getGr10());
        lqw.eq(bo.getGrzh08() != null, Ab05::getGrzh08, bo.getGrzh08());
        lqw.eq(bo.getGrzh09() != null, Ab05::getGrzh09, bo.getGrzh09());
        lqw.eq(bo.getGrzh11() != null, Ab05::getGrzh11, bo.getGrzh11());
        lqw.eq(bo.getGrzh14() != null, Ab05::getGrzh14, bo.getGrzh14());
        lqw.eq(bo.getGrzh16() != null, Ab05::getGrzh16, bo.getGrzh16());
        lqw.eq(bo.getGrzh17() != null, Ab05::getGrzh17, bo.getGrzh17());
        lqw.eq(bo.getGrzh18() != null, Ab05::getGrzh18, bo.getGrzh18());
        lqw.eq(bo.getGrzh19() != null, Ab05::getGrzh19, bo.getGrzh19());
        lqw.eq(bo.getGrzh21() != null, Ab05::getGrzh21, bo.getGrzh21());
        lqw.eq(bo.getGrzh22() != null, Ab05::getGrzh22, bo.getGrzh22());
        lqw.eq(bo.getGrzh23() != null, Ab05::getGrzh23, bo.getGrzh23());
        lqw.eq(bo.getGrzh24() != null, Ab05::getGrzh24, bo.getGrzh24());
        lqw.eq(bo.getGrzh25() != null, Ab05::getGrzh25, bo.getGrzh25());
        lqw.eq(bo.getGrzh26() != null, Ab05::getGrzh26, bo.getGrzh26());
        lqw.eq(bo.getGrzh27() != null, Ab05::getGrzh27, bo.getGrzh27());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab05::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab05::getSystime, bo.getSystime());
        lqw.eq(bo.getGrjx09() != null, Ab05::getGrjx09, bo.getGrjx09());
        lqw.eq(bo.getStatus() != null, Ab05::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab05::getDelStatus, bo.getDelStatus());
        lqw.eq(bo.getCalculationType() != null, Ab05::getCalculationType, bo.getCalculationType());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab05::getFileurl, bo.getFileurl());
        return lqw;
    }

    /**
     * 新增个人账户总账
     *
     * @param bo 个人账户总账
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab05Bo bo) {
        Ab05 add = MapstructUtils.convert(bo, Ab05.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人账户总账
     *
     * @param bo 个人账户总账
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab05Bo bo) {
        Ab05 update = MapstructUtils.convert(bo, Ab05.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab05 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除个人账户总账信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
